package com.acme.bankapp.domain.bank;

import com.acme.bankapp.domain.bank.OverDraftLimitExceededException;

@SuppressWarnings("serial")
public class CheckingAccount extends AbstractAccount {
	private double overdraft;

	public double getOverdraft() {
		return overdraft;
	}
	
	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}
	
	public CheckingAccount() throws IllegalArgumentException {
		super(0);
		overdraft = 0;
	}
	
	public CheckingAccount(double balance) throws IllegalArgumentException
	{
		super(balance);
		overdraft = 0;
	}

	public CheckingAccount(double balance, double overdraft) throws IllegalArgumentException
	{
		super(balance);
		if (overdraft >= 0) {
			this.overdraft = overdraft;
		}
		else {
			throw new IllegalArgumentException();
		}
	}
	
	@Override
	public String toString() {
		return "CheckingAccount [overdraft=" + overdraft + ", balance="
				+ getBalance() + "]";
	}	
	
	@Override
	public void deposit(double x) 
	{
		setBalance(getBalance() + x);
	}
	
	@Override
	public void withdraw(double x) throws OverDraftLimitExceededException {	
 		double balance = getBalance();
		if (balance >= x) 
		{
			setBalance(balance - x);
		} 
		else if (balance + overdraft >= x) 
		{
			overdraft -= ( x - balance );
			setBalance(0);
		}
		else
		{
			throw new OverDraftLimitExceededException(x - (balance+overdraft), this);
		}
	}

	@Override
	public double maximumAmountToWithdraw() {
		return getBalance()+getOverdraft();
	}

}
